import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.*;

import javax.swing.*;

public class PlanesPanel extends JPanel {

	public static PlanesPanel mainPanel;

	public static int width = 800;
	public static int height = 650;
	InputManager input;
	Image backgroundImage;
	
	Player p1;
	Enemy1 e1;
	
	ArrayList<Bullet> bullets;
	boolean triggerDown;

	public PlanesPanel() {
		
		mainPanel = this;
		setFocusable(true);
		requestFocus();
		input = new InputManager();
		addKeyListener(input);
		addMouseMotionListener(input);
		
		backgroundImage = Toolkit.getDefaultToolkit().createImage(
				"Images/Background.png");
		
		prepareImage(backgroundImage, this);
		
		p1 = new Player(350, 560);
		e1 = new Enemy1(350, 30);
		bullets = new ArrayList<Bullet>();
		
	}

	public void paintComponent(Graphics g) {
		draw(g);
		repaint();
	}

	public void draw(Graphics g) {
		update();
		g.drawImage(backgroundImage, 0, 0, null);
		Color view = new Color(255, 255, 255, 80);
		g.setColor(view);
		p1.draw((Graphics2D) g);
		if(e1 == null){
		
		}else{
		e1.draw((Graphics2D) g);
		}
		for (int i = 0; i < bullets.size(); i++) {
			bullets.get(i).draw((Graphics2D) g);
		}

	}

	public double findDistance(int x1, int y1, int x2, int y2) {
		double distance = Math
				.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
		return distance;
	}

	public static double findAngle(double x1, double y1, double x2, double y2) {
		double rise = y2 - y1;
		double run = x2 - x1;
		double arc = Math.atan2(rise, run);
		return arc;
	}

	public void update() {
		//This checks is the Spacebar is down and shoots if it is.
		if (input.isKeyPressed(KeyEvent.VK_SPACE)) {
			
			if (!triggerDown) {
				bullets.add(p1.shoot());
				bullets.add(p1.shoot2());
				triggerDown = true;
				
			}
			
		} else {
			triggerDown = false;
		}
		//End.
		
		//This section checks if if movement keys are pressed and move the plane if they are.
		if (input.isKeyPressed(KeyEvent.VK_W)
				&& input.isKeyPressed(KeyEvent.VK_D) && p1.getY() > 0
				&& (p1.getX() + 100) < width) {
			p1.moveUpRight(Player.baseSpeed);
			
		} else if (input.isKeyPressed(KeyEvent.VK_S)
				&& input.isKeyPressed(KeyEvent.VK_D)
				&& (p1.getY() + 80) < height && (p1.getX() + 100) < width) {
			p1.moveDownRight(Player.baseSpeed);
			
		} else if (input.isKeyPressed(KeyEvent.VK_S)
				&& input.isKeyPressed(KeyEvent.VK_A)
				&& (p1.getY() + 80) < height && p1.getX() > 0) {
			p1.moveDownLeft(Player.baseSpeed);
			
		} else if (input.isKeyPressed(KeyEvent.VK_W)
				&& input.isKeyPressed(KeyEvent.VK_A) && p1.getY() > 0
				&& p1.getX() > 0) {
			p1.moveUpLeft(Player.baseSpeed);
			
		} else if (input.isKeyPressed(KeyEvent.VK_W) && p1.getY() > 0) {
			p1.moveUp(Player.baseSpeed);
			
		} else if (input.isKeyPressed(KeyEvent.VK_S)
				&& (p1.getY() + 80) < height) {
			p1.moveDown(Player.baseSpeed);
			
		} else if (input.isKeyPressed(KeyEvent.VK_A) && p1.getX() > 0) {
			p1.moveLeft(Player.baseSpeed);
			
		} else if (input.isKeyPressed(KeyEvent.VK_D)
				&& (p1.getX() + 100) < width) {
			p1.moveRight(Player.baseSpeed);
			
		}
		//End
		
		if(e1 == null){
			
		}else{
		e1.moveDown(Enemy1.baseSpeed);
		}
		
		//This section updates the bullits and checks if they are out of the screen.
		for (int i = 0; i < bullets.size(); i++) {
			bullets.get(i).update();
		}
		for (int i = 0; i < bullets.size(); i++) {
			
				if (bullets.get(i).getY() < 0) {
					bullets.remove(i);
					break;
				}
			

		}
		//End
		
		//This section updates the player.
		p1.movePlayer(input.getMouseX(), input.getMouseY());
		//End
		
		if(e1 == null){
			
		}else{
		e1.movePlayer(input.getMouseX(), input.getMouseY());
		}
		
		if(e1 == null){
			
		}else{
		if(e1.getY()>650){
			e1 = null;
		}
		}
		
//		for (int i = 0; i < bullets.size(); i++) {
//			if(bullets.get(i).collisionCheck(e1)){
//			bullets.remove(i);
//			e1 = null;
//			}
//		}
		
	}

}